package cards;
import java.util.Scanner;



public class Game {
	
	
	public static boolean repeat = false;
	static Player player = new Player();
	static Deck deck = new Deck();
	static Scanner scan = new Scanner(System.in);
	
	public static void restartGame() {
		repeat = false;
		deck = new Deck();
		player.currentHand = new BlackjackHand();

	}
	public static OtherPlayer createOtherPlayer(String i) {
		OtherPlayer otherplayer = new OtherPlayer();
		otherplayer.setName(i);
		otherplayer.currentHand.addCard(deck.draw(2));
		otherplayer.playerAI(deck);
		System.out.println(otherplayer.name);
		System.out.println(otherplayer.currentHand);
		return otherplayer;
	}
	
	public static boolean betChecker(int bet, int balance) {
		
		if (bet > balance) {
			System.out.println("What do I look like, your bank? Get more money you fuckin' jew!");
			return true;
		} else 
			return false; 
	}
	
	public static void playAgain() {
		
		if(player.bank.checkBalance() == 0) {
			System.out.println("Your out of cash, get out of the casino before I kick your jew ass out myself");
			repeat = true;
		} else {
			System.out.println("Would you like to keep playing cunt? (1) Yes, (2) Quit cos your a faggot");
			System.out.println("Response:");
			int response = scan.nextInt();
			if(response == 2) {
				repeat = true;
			} else {
				restartGame();		
			}
		}

		
		
	}

	public static String resultChecker(BlackjackHand hand, BlackjackDealer dealer, Player player) {

		System.out.println("\n"+dealer.toString());
		String gameover = "\n\nGame over! ";
		
		if(hand.getScore() == 21) {
			
			player.bank.addCash(player.bet.returnBlackjackBet(player.bet.getBet()));
			return gameover+"BLACKJACK!";
						
		} else if (hand.getScore() < 21) {
			if(hand.getScore() > dealer.currentHand.getScore() || dealer.currentHand.getScore() > 21) {
				player.bank.addCash(player.bet.returnStandardBet(player.bet.getBet()));
				gameover += "YOU WON! ";
			} else {
				gameover += "YOU ARE THE WORST! ";
			}
			
			return gameover+"You scored " + hand.getScore();
		
		} else {
			return gameover + "YOU WENT BUST!";
		}
		
	}
	
	public static void main(String[] args) {
		System.out.println("Hello, what is your name?");
		String nameInput = scan.next();
		if(nameInput != "") {
			player.setName(nameInput);
		}
		System.out.println("Hello "+player.getName()+".\n");
		System.out.println("Setting up line of credit...");
		player.bank.addCash(50);
		System.out.println("Your bank balance is: "+player.bank.checkBalance());
		System.out.println("\nShuffling deck...");
		System.out.println("How many people would you like to play against?");
		int players = scan.nextInt();
		for(int i = 0; i< players; i++) {
			String name = "John";
			name = name + i;
			createOtherPlayer(name);
		}
		
		
		while (!repeat) {
			boolean continueGame = true;
			
			int validBet = 0;
			while (validBet == 0) {
				System.out.println("How much would you like to bet?");
				int amount = scan.nextInt();

				if(betChecker(amount, player.bank.checkBalance()) == false) {
					player.bet.setBet(amount);
					player.bank.subtractCash(amount);
					validBet = 1;
					
				} else 
					validBet = 0;

			}
						
			System.out.println("Your bet: " + player.bet.getBet());
			System.out.println("Your balance: " + player.bank.checkBalance());		
			System.out.println("Dealing your cards...\n");
			
			BlackjackDealer dealer = new BlackjackDealer();
			dealer.currentHand.addCard(deck.draw(2));
			System.out.println(dealer.toString());
			System.out.println("\n");
			player.currentHand.addCard(deck.draw(2));
			System.out.println(player.currentHand.toString());
			
			while(continueGame) {
				System.out.println("\nDealer asks 'Will you hit (1) or stay (2)?'");
				System.out.println("Response: ");
				int input = scan.nextInt();
				
				if(input == 1) {
					player.currentHand.addCard(deck.draw());
					System.out.println(player.currentHand.toString());
					if(player.currentHand.getScore()>21) {
						continueGame = false;
						dealer.dealerAI(deck);
						
						
						System.out.println(resultChecker(player.currentHand, dealer, player));	
						System.out.println("Your balance: " + player.bank.checkBalance());		
						playAgain();
					}
					
				} else {
					continueGame = false;
					dealer.dealerAI(deck);
					System.out.println(resultChecker(player.currentHand, dealer, player));	
					System.out.println("Your balance: " + player.bank.checkBalance());
					playAgain();
				}
			
						
				}
			

			
		} //while repeat loop terminates here
		
	}
		
	
}
